<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_daftar_penduduk extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'no_ktp';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$no_ktp = isset($_POST['no_ktp']) ? strval($_POST['no_ktp']) : '';
		$nama = isset($_POST['nama']) ? strval($_POST['nama']) : '';
		$agama = isset($_POST['agama']) ? strval($_POST['agama']) : '';
		$gol_darah = isset($_POST['gol_darah']) ? strval($_POST['gol_darah']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*, date_format(tgl_lahir, "%d/%m/%Y") as lahir', FALSE);
			$this->db->from('warga');
			$this->db->where('status','1');
			
			#Kimochi - otoritas
				#--Faklutas
			/* if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			*/

			if($no_ktp != ''){
				$this->db->like('no_ktp', $no_ktp, 'both');
			}
			
			if($nama != ''){
				$this->db->like('nama', $nama, 'both');
			}
			
			if($agama != ''){
				$this->db->where('agama', $agama);
			}
			
			if($gol_darah != ''){
				$this->db->where('gol_darah', $gol_darah);
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('warga');
		
		if($status=='ma'){
			$this->db->where('status', '1');
		}
		return $this->db->get()->num_rows();
	}

	function getDataGraph($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('warga');
		
		$this->db->where('status', '1');
		
		return $this->db->get();
	}
	
	function getDataChart(){
		// $i=0;
		// foreach($data as $d){
		$this->db->flush_cache();
		$this->db->select('thn_daftar, COUNT(thn_daftar) as jumlah');
		$this->db->from('warga');
		$this->db->where('status', '1');
		$this->db->group_by('thn_daftar');
		
		$res = $this->db->get();

        if($res->num_rows>0){
            $tmp = $res->result_array();
        }else{
            $tmp['thn_daftar']= '2009';
            $tmp['jumlah']= 0;
        }
 		//   $i++;
		// }
		return $tmp;
	}

	function getDataChartJK($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('j_kel', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartJKSum($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('j_kel', $d);
			
			$tmp += $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartAgama($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('agama', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartPendidikan($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('pendidikan', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartPekerjaan($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('pekerjaan', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('no_ktp',$kode);
		
		$this->db->select('*');
		$this->db->from('warga');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('no_ktp', $data['no_ktp']);
		$this->db->set('nama', $data['nama']);
		$this->db->set('agama', $data['agama']);
		$this->db->set('t_lahir', $data['t_lahir']);
		$this->db->set('tgl_lahir', FormatDateToMysql($data['tgl_lahir']));
		$this->db->set('j_kel', $data['j_kel']);
		$this->db->set('gol_darah', $data['gol_darah']);
		$this->db->set('w_negara', $data['w_negara']);
		$this->db->set('pendidikan', $data['pendidikan']);
		$this->db->set('pekerjaan', $data['pekerjaan']);
		$this->db->set('s_nikah', $data['s_nikah']);
		$this->db->set('thn_daftar', $data['thn_daftar']);
		//$this->db->set('status', $data['status']);
		$result = $this->db->insert('warga');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}

	function getdatadetail($no_ktp){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('warga');
		//$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		
		$this->db->where('no_ktp', $no_ktp);
		return $this->db->get();
	}

	#export excel
	function getDataExcel(){
		$this->db->select('*');
		$this->db->from('warga');
		$this->db->where('status', '1');

		return $this->db->get();
	}

	function getDataChartUmurB($data){
		$i=0;
		foreach ($data as $d) {
			# code...
            $this->db->flush_cache();
            $this->db->select("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS age", FALSE);
            $this->db->from('warga');
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) >= 0");
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) <= 5");
        	
        	$tmp[$i] = $this->db->get()->num_rows();
        	$i++;
        }
		return $tmp;
	}

	function getDataChartUmurA($data){
		$i=0;
		foreach ($data as $d) {
			# code...
		    $this->db->flush_cache();
            $this->db->select("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS age", FALSE);
            $this->db->from('warga');
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) > 5");
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) <= 14");
            
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartUmurR($data){
		$i=0;
		foreach ($data as $d) {
			# code...
            $this->db->flush_cache();
            $this->db->select("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS age", FALSE);
            $this->db->from('warga');
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) > 14");
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) <= 20");
                    
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartUmurD($data){
		$i=0;
		foreach ($data as $d) {
			# code...
            $this->db->flush_cache();
            $this->db->select("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS age", FALSE);
            $this->db->from('warga');
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) > 20");
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) <= 30");
        
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}

	function getDataChartUmurT($data){
		$i=0;
		foreach ($data as $d) {
			# code...
		    $this->db->flush_cache();
            $this->db->select("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) AS age", FALSE);
            $this->db->from('warga');
            $this->db->where("TIMESTAMPDIFF(YEAR, tgl_lahir, CURDATE()) > 30");
            
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}
	
}

?>